package com.kzd.sdk.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.kzd.sdk.base.OwnerActivity;
import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.ui.fragment.KZD_HomeCouponFragment;
import com.kzd.sdk.ui.fragment.KZD_HomeGiftFragment;
import com.kzd.sdk.ui.fragment.KZD_HomeMoveFragment;
import com.kzd.sdk.ui.fragment.KZD_HomeServiceFragment;
import com.kzd.sdk.ui.fragment.KZD_HomeUserFragment;
import com.kzd.sdk.utils.AppUtils;

import java.util.LinkedList;
import java.util.List;

public class KZD_HomeActivity extends OwnerActivity {

    public static void open() {
        Activity activity = KZD_SdkController.getActivity();
        if (activity == null) {
            return;
        }
        Intent intent = new Intent(activity, KZD_HomeActivity.class);
        activity.startActivity(intent);
    }

    private TextView tvKZDHomeTitle;
    private ViewPager vpKZDHomeBody;
    private RadioGroup rgKZDHomeRadioGroup;
    private RadioButton rbKZDHomeRadioUser, rbKZDHomeRadioGame, rbKZDHomeRadioGift, rbKZDHomeRadioCoupon, rbKZDHomeRadioMake;

    @Override
    public String getLayoutIdByName() {
        return "kzd_activity_home";
    }

    @Override
    public View getLayoutView() {
        return null;
    }

    @Override
    public void initView() {

        tvKZDHomeTitle = findViewById(AppUtils.getId((this), ("tvKZDHomeTitle")));
        vpKZDHomeBody = findViewById(AppUtils.getId((this), ("vpKZDHomeBody")));
        rgKZDHomeRadioGroup = findViewById(AppUtils.getId((this), ("rgKZDHomeRadioGroup")));
        rbKZDHomeRadioUser = findViewById(AppUtils.getId((this), ("rbKZDHomeRadioUser")));
        rbKZDHomeRadioGame = findViewById(AppUtils.getId((this), ("rbKZDHomeRadioGame")));
        rbKZDHomeRadioGift = findViewById(AppUtils.getId((this), ("rbKZDHomeRadioGift")));
        rbKZDHomeRadioCoupon = findViewById(AppUtils.getId((this), ("rbKZDHomeRadioCoupon")));
        rbKZDHomeRadioMake = findViewById(AppUtils.getId((this), ("rbKZDHomeRadioMake")));

        findViewById(AppUtils.getId((this), "ivBackPressed"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });

        rgKZDHomeRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                selectRadioToFragment(checkedId);
            }
        });

        vpKZDHomeBody.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectFragmentToRadio(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initData() {
        //默认选中“个人中心”
        rbKZDHomeRadioUser.setChecked(true);

        List<Fragment> fragments = new LinkedList<>();
        fragments.add(new KZD_HomeUserFragment());
        fragments.add(new KZD_HomeMoveFragment());
        fragments.add(new KZD_HomeGiftFragment());
        fragments.add(new KZD_HomeCouponFragment());
        fragments.add(new KZD_HomeServiceFragment());

        HomePagerAdapter pagerAdapter = new HomePagerAdapter(
                getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, fragments
        );
        vpKZDHomeBody.setAdapter(pagerAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void selectRadioToFragment(int checkedId) {
        if (checkedId == AppUtils.getId((this), ("rbKZDHomeRadioGame"))) {
            vpKZDHomeBody.setCurrentItem(1);
        } else if (checkedId == AppUtils.getId((this), ("rbKZDHomeRadioGift"))) {
            vpKZDHomeBody.setCurrentItem(2);
        } else if (checkedId == AppUtils.getId((this), ("rbKZDHomeRadioCoupon"))) {
            vpKZDHomeBody.setCurrentItem(3);
        } else if (checkedId == AppUtils.getId((this), ("rbKZDHomeRadioMake"))) {
            vpKZDHomeBody.setCurrentItem(4);
        } else {
            vpKZDHomeBody.setCurrentItem(0);
        }
        tvKZDHomeTitle.setText(((RadioButton) findViewById(checkedId)).getText());
    }

    private void selectFragmentToRadio(int position) {
        if (position == 1) {
            rbKZDHomeRadioGame.setChecked(true);
        } else if (position == 2) {
            rbKZDHomeRadioGift.setChecked(true);
        } else if (position == 3) {
            rbKZDHomeRadioCoupon.setChecked(true);
        } else if (position == 4) {
            rbKZDHomeRadioMake.setChecked(true);
        } else {
            rbKZDHomeRadioUser.setChecked(true);
        }
    }

    private static class HomePagerAdapter extends FragmentStatePagerAdapter {

        private final List<Fragment> fragments = new LinkedList<Fragment>();

        public HomePagerAdapter(FragmentManager fm, int behavior, List<Fragment> fragments) {
            super(fm, behavior);
            this.fragments.clear();
            this.fragments.addAll(fragments);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

        }
    }
}
